{% extends 'index.html' %}

{% block content %}
    <h2>编辑任务</h2>
    <form method="POST">
        <div class="mb-3">
            <label for="task_name" class="form-label">任务名称</label>
            <input type="text" class="form-control" id="task_name" name="task_name" value="{{ task['task_name'] }}" required>
        </div>
        <div class="mb-3">
            <label for="config_ids" class="form-label">选择配置文件</label>
            <select class="form-select" id="config_ids" name="config_ids" multiple="multiple" required>
                {% for config in configs %}
                    <option value="{{ config[0] }}" {% if config[0]|string in selected_config_ids %}selected{% endif %}>
                        {{ config[1] }} (ID: {{ config[0] }})
                    </option>
                {% endfor %}
            </select>
        </div>
        <div class="mb-3">
            <label for="interval_type" class="form-label">时间间隔类型</label>
            <select class="form-select" id="interval_type" name="interval_type" required>
                <option value="minute" {% if task['interval_type'] == 'minute' %}selected{% endif %}>每分钟</option>
                <option value="hourly" {% if task['interval_type'] == 'hourly' %}selected{% endif %}>每小时</option>
                <option value="daily" {% if task['interval_type'] == 'daily' %}selected{% endif %}>每日</option>
                <option value="weekly" {% if task['interval_type'] == 'weekly' %}selected{% endif %}>每周</option>
                <option value="monthly" {% if task['interval_type'] == 'monthly' %}selected{% endif %}>每月</option>
            </select>
        </div>
        <div class="mb-3">
            <label for="interval_value" class="form-label">时间间隔值</label>
            <input type="number" class="form-control" id="interval_value" name="interval_value" value="{{ task['interval_value'] }}" required>
            <small id="intervalHelp" class="form-text text-muted">请输入有效的间隔值。</small>
        </div>
        <div class="mb-3">
            <label for="task_mode" class="form-label">任务模式</label>
            <select class="form-select" id="task_mode" name="task_mode" required>
                <option value="strm_creation" {% if task['task_mode'] == 'strm_creation' %}selected{% endif %}>运行 STRM 创建</option>
                <option value="strm_validation_quick" {% if task['task_mode'] == 'strm_validation_quick' %}selected{% endif %}>STRM 文件有效性检测（快扫）</option>
                <option value="strm_validation_slow" {% if task['task_mode'] == 'strm_validation_slow' %}selected{% endif %}>STRM 文件有效性检测（慢扫）</option>
            </select>
        </div>
        <div class="mb-3">
            <label for="is_enabled" class="form-label">是否启用</label>
            <select class="form-select" id="is_enabled" name="is_enabled" required>
                <option value="1" {% if task['is_enabled'] %}selected{% endif %}>启用</option>
                <option value="0" {% if not task['is_enabled'] %}selected{% endif %}>禁用</option>
            </select>
        </div>
        <button type="submit" class="btn btn-primary">保存任务</button>
    </form>

    <!-- 引入 Select2 CSS 和 JS -->
    <link href="{{ url_for('static', filename='css/select2.min.css') }}" rel="stylesheet">
    <script src="{{ url_for('static', filename='js/jquery-3.6.0.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/select2.min.js') }}"></script>

    <script>
        $(document).ready(function() {
            $('#config_ids').select2({
                placeholder: "请选择配置文件",
                allowClear: true
            });

            function updateIntervalValueRange() {
                var intervalValueInput = document.getElementById('interval_value');
                var intervalHelp = document.getElementById('intervalHelp');
                var intervalType = document.getElementById('interval_type').value;
                if (intervalType == 'minute') {
                    intervalValueInput.min = 1;
                    intervalValueInput.max = 59;
                    intervalHelp.textContent = '分钟间隔值必须在 1 到 59 之间。';
                } else if (intervalType == 'hourly') {
                    intervalValueInput.min = 1;
                    intervalValueInput.max = 23;
                    intervalHelp.textContent = '小时间隔值必须在 1 到 23 之间。';
                } else if (intervalType == 'daily') {
                    intervalValueInput.min = 1;
                    intervalValueInput.max = 31;
                    intervalHelp.textContent = '天数间隔值必须在 1 到 31 之间。';
                } else if (intervalType == 'weekly') {
                    intervalValueInput.min = 0;
                    intervalValueInput.max = 6;
                    intervalHelp.textContent = '星期值必须在 0（周日）到 6（周六）之间。';
                } else if (intervalType == 'monthly') {
                    intervalValueInput.min = 1;
                    intervalValueInput.max = 12;
                    intervalHelp.textContent = '月份间隔值必须在 1 到 12 之间。';
                }
            }

            document.getElementById('interval_type').addEventListener('change', updateIntervalValueRange);
            // 页面加载时调用一次
            updateIntervalValueRange();
        });
    </script>

{% endblock %}
